﻿namespace LogManager.Gui.ViewModels
{
    using LogManager.Core.Infrastructure.Logging;
    using LogManager.Core.Models;
    using LogManager.Gui.Helpers;
    using LogManager.Gui.Views;
    using LogManager.Utils.Unity;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows.Input;
    using LogManager.Gui.Factories;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class WarehouseViewModel : ViewModelBase<IWarehouseView>, IWarehouseViewModel
    {
        public IWarehouse Warehouse { get; private set; }
        public ReadOnlyObservableCollection<IPlateformViewModel> PlateformsVm { get; private set; }
        public readonly IMainViewModel MainViewModel;
        public WarehouseViewModel(IWarehouseView view, IMainViewModel mainViewModel, IWarehouse warehouse, IViewModelFactory viewModelFactory)
            : base(view)
        {
            this.MainViewModel = mainViewModel;
            this.Warehouse = warehouse;
            this.PlateformsVm = Warehouse.Plateforms.GetSyncWrapper(m => viewModelFactory.CreatePlateformViewModel(m));
        }

        #region Commands

        public ICommand DeleteWarehouseCommand { get { return new DelegateCommand(OnDeleteWarehouse); } }
        private void OnDeleteWarehouse()
        {
            this.Warehouse.DeleteWarehouse();
            Log.Logger.AddInfo("Entrepôt supprimé", this.Warehouse.Name);
        }

        public ICommand AddPlateformCommand { get { return new DelegateCommand(OnAddPlateform); } }
        private void OnAddPlateform()
        {
            var newplateform = this.Warehouse.AddNewPlateform();
            this.MainViewModel.View.ExpandAndSelectItem(PlateformsVm.Single(p => p.Plateform == newplateform));
            Log.Logger.AddInfo("Plateforme créée", newplateform.Name);
        }

        #endregion
    }


}
